<template>
  <div :id="id"
    :class="className"
    :style="{ height: height, width: width }" />
</template>

<script>
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    id: { type: String, default: 'chart' },
    className: { type: String, default: 'chart' },
    height: { type: String, default: '90%' },
    width: { type: String, default: '100%' },
    message: Object
  },
  data() {
    return {
      chart: null,
      options: this.message ? this.message : {}
    }
  },
  watch: {
    message: {
      handler(newValue) {
        this.options = newValue
        this.chart?.setOption(this.options, true)
      }
    },
    id() {
      this.initChart()
    },
    deep: true
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      let _this = this
      this.chart = this.$echarts.init(document.getElementById(this.id))
      // 切换图列后触发
      this.chart.on('legendselectchanged', function (params) {
        _this.$emit('clickSessionChart', params)
      })
      // 点击图表
      this.chart.on('click', function (params) {
        _this.$emit('clickChartItem', params)
      })
      this.chart.setOption(this.options, true)
      // this.$nextTick(() => {
      //   this.sendBase64();
      // });
      // setTimeout(this.sendBase64(), 100000);
    },
    sendBase64() {
      this.$emit('getBase64', this.chart.getDataURL())
    }
  }
}
</script>
